package com.ruyuan.token;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

/**
 * @author xx
 */
@Component
public class TokenBeanPostProcessor implements BeanPostProcessor, Ordered {

    private ConfigurableApplicationContext ctx;

    public TokenBeanPostProcessor(ConfigurableApplicationContext context) {
        this.ctx = context;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof TokenUtilAware) {
            ((TokenUtilAware)bean).setTokenUtil(ctx.getBean(TokenUtil.class));
        }
        return bean;
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
